import json
import os
from itertools import groupby
class GenContext:
    def __init__(self, entries,tables):
        self.entries = entries
        self.tables = tables
        
    def _purge_entry(self):
        url_exist = set()
        for entry in self.entries:
            if entry['request']['url'].endswith(('png','js','css','ico','woff2','woff','ttf','jpg','svg')):
                continue
            #if 'baidu' not in entry['request']['url']:
            #    continue
            if entry['request']['url'] not in url_exist:
                res = dict()
                headers = self._purge_headers(entry)
                response = self._purge_response(entry)
                body = self._purge_body(entry)
                res['URL'] = entry['request']['url']
                res['HEADER'] = headers
                res['BODY'] = body
                res['RESPONSE'] = response
                yield res
                url_exist.add(entry['request']['url'])
    
    def _purge_headers(self,entry):
        new_entry = dict()
        keys = ['name']
        for key in entry['request']['headers'][0]:
            if key in keys:
                new_entry[key] = entry['request']['headers'][0][key]
        return new_entry
    
    def _purge_response(self,entry):
        new_entry = dict()
        keys = ['status']
        for key in entry['response']:
            if key in keys:
                new_entry[key] = entry['response'][key]
        return new_entry
    
    def _purge_body(self,entry):
        return 'body'
    
    def _classify(self):
        res = list()
        for table in self.tables:
           res.append(dict(table))
        for entry in self._purge_entry():
            res.append(entry)
        res = sorted(res, key= lambda item: item['URL'])
        for item in groupby(res,key=lambda item:item['URL']):
            yield item